
<html>
    <head>
        <title> Mad LuLan </title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <script type="text/javascript" src="public/js/checking.is"></script>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquety.js"></script>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    </head>

    <body>
        <?php
        require_once 'model/db.php';
        ?>

        <h1>Mad LuLan</h1>

        <?php
        $query = "SELECT id, name FROM companies";
        $companies = getData($query);
        $apps = getApps("SELECT * FROM apps");
        ?>

        <link href="public/css/page.css" type="text/css" rel="stylesheet">
        <div id="khung">
            <form method="post">
                <div id="dieukhien">
                    Key World: <input type="text" name="keyw" id="keyw" placeholder="cafe" ><br>
                    Min Fans: <input type="text" name="min" id="min" placeholder="1" ><br>
                    Max Fans: <input type="text" name="max" id="max" placeholder="10" ><br>
                    Companies: <select name="id_company">
                        <option value="0">--- Company ---</option>
                        <?php foreach ($companies as $company) : ?>
                            <option value="<?php echo $company['id']; ?>"><?php echo $company["name"]; ?></option>
                        <?php endforeach; ?>
                    </select><br>
                    Apps: <select name="app">
                        <option value="0">---- A ----</option>
                        <?php foreach ($apps as $app) : ?>
                            <option value="<?php echo $app['id']; ?>"><?php echo $app["name"]; ?></option>
                        <?php endforeach; ?>
                    </select><br>
                    <input type="submit" name="submit" value="Search">
                    <input type="button" name="submit" id="check" value="Check">
                </div>
            </form>

            <?php if (isset($_POST['submit'])): ?>
                <?php
                $min_fans = $_POST['min']; //TODO
                $max_fans = $_POST['max']; //TODO
                $keyword = $_POST['keyw']; //TODO
                $company = $_POST["id_company"];
                $app = $_POST["app"];
                if (!empty($max_fans))
                    $max_fans = "(likes <= '$max_fans') AND";
                else
                    $max_fans = "";
                if (!empty($min_fans))
                    $min_fans = "(likes >= '$min_fans') AND";
                else
                    $min_fans = "";
                if (!empty($company))
                    $company = "(companies.id='$company') AND";
                else
                    $company = "";
                if (!empty($app))
                    $app = "(apps.id='$app') AND";
                else
                    $app = "";
                $query = "SELECT * FROM pages
                INNER JOIN apps_pages ON apps_pages.id_page = pages.id
                INNER JOIN apps ON apps.id = apps_pages.id_app
                INNER JOIN companies ON apps.id_company=companies.id
                  WHERE
                    $min_fans
                       $max_fans
                           $company
                               $app
                     (description LIKE '% $keyword %')
                  LIMIT 0, 10";
                var_dump($query);

                $pages = getData($query);
                ?>
                <?php if (!empty($pages)): ?>
                    <table>
                        <tr>
                            <th> picture </th>
                            <th> pid </th>
                            <th> id </th>
                            <th> name </th>
                            <th> likes </th>
                            <th> links </th>
                            <th> description </th>
                        </tr>
                        <?php foreach ($pages as $page): ?>
                            <tr>
                                <td><img src='<?php echo $page["picture"] ?>'></td>
                                <td><?php echo $page['pid'] ?></td>
                                <td><?php echo $page['id'] ?></td>
                                <td><?php echo $page['name'] ?></td>
                                <td><?php echo $page['likes'] ?></td>
                                <td><a href='<?php echo $page["link"] ?>'><?php echo substr(($page["link"]), 0, 35) ?></td>
                                <td><?php echo substr(($page['description']), 0, 100) ?></td>
                            </tr>
                        <?php endforeach; ?>
                    </table>
                <?php endif; ?>
            <?php endif; ?>
        </div>

        <script>
            //var text_key = document.getElementById('keyw').value;
            // var text_min = document.getElementById('min').value;
            // var text_max = document.getElementById('max').value;
            // alert(" Keyw fans is : " + text_key);
            //alert(" Min fans is : " + text_min);
            //alert(" Max fans is : " + text_max);

            $(document).ready(function() {
                $('#check').click(function() {
                    var min_fans = $('#min').val();
                    var flag = checkNumber(min_fans);
                    //Kiem tra so
                    if (flag) {
                        alert('Is Number');
                    } else {
                        alert('Is not Number');
                    }
                    //alert($("#min_fans").val());
                });
                function checkNumber(val) {
                    if ($.isNumeric(val)) {
                        return true;
                    } else {
                        return false;
                    }

                }
                ;
            });
        </script>

    </body>

</html>
